package com.ltg.file.local.service.impl;

import com.ltg.file.config.FileProperties;
import com.ltg.file.entity.FileInfo;
import com.ltg.file.exception.FileException;
import com.ltg.file.local.conf.FileLocalProperties;
import com.ltg.file.sevice.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;

/**
 * <p> ClassName: LocalFileSeviceImpl </p>
 * <p> Package: com.ltg.file.local.service.impl </p>
 * <p> Description: </p>
 * <p></p>
 *
 * @Author: LTG
 * @Create: 2023/5/20 - 21:26
 * @Version: v1.0
 */
@Service
public class LocalFileServiceImpl implements FileService {

    @Autowired
    private FileLocalProperties fileLocalProperties;

    @Autowired
    private FileProperties fileProperties;

    @Override
    public FileInfo upload(MultipartFile file) {
        String id = UUID.randomUUID().toString();
        if (file == null) {
            throw new FileException("文件为空");
        }
        String originName = file.getOriginalFilename();
        String contentType = file.getContentType();
        String extensionName = null;
        if (originName != null) {
            extensionName = originName.substring(originName.lastIndexOf("."));
        }
        File toFile = new File(fileLocalProperties.getPath() + File.separator + id + extensionName);
        String fileName = id + extensionName;
        String absolutePath = null;
        try {
            absolutePath = toFile.getCanonicalPath();

            String dirPath = absolutePath.substring(0, absolutePath.lastIndexOf(File.separator));
            File dir = new File(dirPath);
            if (!dir.exists()) {
                boolean mkdirs = dir.mkdirs();
            }
            InputStream ins = file.getInputStream();
            inputStreamToFile(ins, toFile);
            ins.close();
        } catch (IOException e) {
            throw new FileException(e.getMessage());
        }
        FileInfo fileInfo = new FileInfo();
        fileInfo.setDrive("LOCAL");
        fileInfo.setOriginName(originName);
        fileInfo.setContentType(contentType);
        fileInfo.setExtensionName(extensionName);
        fileInfo.setFileSize(file.getSize());
        fileInfo.setPath(absolutePath);
        fileInfo.setFileName(fileName);
        fileInfo.setPath(absolutePath);
        fileInfo.setUrl(fileProperties.getDomain() + fileLocalProperties.getUrl() + "/" + id);
        fileInfo.setDownloadUrl(fileProperties.getDomain() + fileLocalProperties.getUrl() + "/" + id);
        return fileInfo;
    }

    private void inputStreamToFile(InputStream ins, File file) {
        OutputStream os = null;

        try {
            os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                throw new FileException(e.getMessage());
            }
        }
    }
}
